﻿using System.Windows.Forms;

namespace RSuite.R5
{
    public class MainForm : WebKitForm
    {
        private RankFix rankFix;

        public MainForm()
            : base(@"UI/R5.htm")
        {
        }

        protected override void InitForm(Form form)
        {
            form.Text = "R5 - Rank document";
            form.Icon = new System.Drawing.Icon(@"UI\images\r4.ico");
            form.Width = 300;
            form.Height = 400;
            form.MinimumSize = form.Size;
        }

        protected override object InvokeLocalMethod(string method, dynamic data, string id)
        {
            switch (method)
            {
                case "open":
                    return this.Open(data);
                case "start":
                    this.rankFix.Fix(id);
                    return string.Empty;
                case "cancel":
                    this.rankFix.Cancel();
                    return string.Empty;
                default:
                    return string.Empty;
            }
        }

        private object Open(string filename)
        {
            var dialog = new OpenFileDialog();
            dialog.FileName = filename;
            if (base.ShowDialog(dialog) != DialogResult.OK)
            {
                return string.Empty;
            }
            this.rankFix = new RankFix(dialog.FileName);
            this.rankFix.Update += new RankFix.UpdateEventHandler(rankFix_Update);
            return dialog.FileName;
        }

        private void rankFix_Update(string id, int type, string content)
        {
            base.JobUpdate(id, new
            {
                Type = type,
                Content = content
            });
        }
    }
}